/*
 * @Author: wwssaabb
 * @Date: 2021-12-04 09:36:23
 * @LastEditTime: 2021-12-08 16:07:11
 * @FilePath: \handwritten-code\vue\dep.js
 */

//Dep 依赖收集
let uid = 0

export default class Dep {
  constructor() {
    this.uid = uid++ //每一个dep都是独一无二的
    this.subs = [] //用来存储watcher的数组
  }

  //添加依赖
  depend() {
    if (Dep.target) {
      Dep.target.addDep(this)
    }
  }

  //通知更新
  notify() {
    const tempSubs = this.subs.slice()
    tempSubs.reverse().forEach(watcher => watcher.update())
  }

  //将依赖添加到subs
  addDep(watcher) {
    this.subs.push(watcher)
  }
}

Dep.target = null